'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 折扣商城表
 * @author xiaoxu
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');
var envUtil = require('../util/envUtil');
var mgrUtil = require('../util/mgrUtil');
var eventType = require('../consts/eventType.js');

/**
 * 折扣商城表
 */
var discountSchema = new mongoose.Schema({
    // id
    role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 折扣商城次数
    discount_shop_num_list: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    }
});

/**
 * 增加折扣商城已购买type
 * @return {[type]} [description]
 */
discountSchema.methods.addDiscountShopNum = function (shop_id) {
    this.discount_shop_num_list[shop_id] = this.discount_shop_num_list[shop_id] || 0;
    this.discount_shop_num_list[shop_id]++;
    this.markModified('discount_shop_num_list');
};

/**
 * 获取折扣商城已购买type 次数
 * @return {[type]} [description]
 */
discountSchema.methods.getDiscountShopNum = function (shop_id) {
    return this.discount_shop_num_list[shop_id] || 0;
};

/**
 * 刷新
 * @return {[type]} [description]
 */
discountSchema.methods.refresh = function () {
    this.discount_shop_num_list = {};
    this.markModified('discount_shop_num_list');
};

discountSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 获取修改
 * @return {[type]} [description]
 */
discountSchema.methods.getLastChange = function () {

};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
discountSchema.methods.push = function () {
    this.pushModify();
};

discountSchema.methods.pushModify = function () {

};

if (!discountSchema.options.toJSON) {
    discountSchema.options.toJSON = {};
}
/* jshint unused:false */
discountSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Discount', discountSchema);
